<?php
class Onlookers extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	$limit = 20;
    	
    	$this->load->model('common/model_onlookers_conf','onlookers_conf');
    	$total_rows = $this->onlookers_conf->count();
    	
    	
    	//分页
    	$this->load->library('pagination');
    	
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $limit;
    	$config['base_url'] = '/operate/onlookers/index';
    	
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    	
    	
    	//读取数据
    	$offset = ($page-1) * $config['per_page'];
        $lists = $this->onlookers_conf->lists($offset, $limit);

        $data = array(
            'lists' => $lists,
        	'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        );
        
        $this->load->view('admin/operate/onlookers/index', $data);
    }
    
    /*
     * 添加
    * */
    public function add()
    {
    	$this->load->view('admin/operate/onlookers/add');
    }
    
    /*
     * 编辑
    * */
    public function edit()
    {
    	$cid = intval($this->input->get_post('cid'));
    	
    	$this->load->model('common/model_onlookers_conf','onlookers_conf');
    	$info = $this->onlookers_conf->getById($cid);
    	
    	$this->load->view('admin/operate/onlookers/add', array('info'=>$info));
    }
    
    public function save()
    {
    	$cid = intval($this->input->get_post('cid'));
    	$aid = intval($this->input->get_post('aid'));
    	$name = trim($this->input->get_post('name'));
    	$effective_time = trim($this->input->get_post('effective_time'));
    	 
    	$this->load->model('common/model_onlookers_conf','onlookers_conf');
    	 
    	$data = array(
    			'aid'=>$aid,
    			'name'=>$name,
    			'effective_time'=>$effective_time,
    			'create_time'=>date('Y-m-d H:i:s'),
    	);


        $rs_save = $this->onlookers_conf->save($data, $cid ? $cid : null);

    	
    	$this->load->helper('url');
    	$url = url('admin').'operate/onlookers/index';
    	redirect($url);
    }
    
    /*
     * 赠送
    * */
    public function give()
    {
    	set_time_limit(0);
    	
    	$cid = intval($this->input->get_post('cid'));
    	$uid = intval($this->input->get_post('uid'));
    	$source = intval($this->input->get_post('source'));
    	$offset = intval($this->input->get_post('offset'));
    	
    	$this->load->model('common/model_onlookers_conf','onlookers_conf');
    	$onlookers_conf = $this->onlookers_conf->getById($cid);
    	
    	if($uid || $source)
    	{
    		$this->load->model('user/model_user', 'u');
    		
    		$where = array();
    		
    		if($uid)
    		{
    			$where['uid'] = $uid;
    		}
    		if($source)
    		{
    			$where['source'] = $source;
    		}
    		
    		$offset = $offset ? $offset : OFFSET;
    		$limit = 20;
    		
    		$lists = $this->u->lists($offset, $limit, $where);
    		
    		$this->load->model('common/Model_onlookers', 'o');
    		$this->load->service('message/service_message_push');
    		
    		$end_time = date('Y-m-d H:i:s', strtotime('+90 days'));
    		
    		foreach($lists as $v)
    		{
    			$rs_give = $this->o->give($v['uid'], $cid);
    			if($rs_give['status'] == 200)
    			{
    				$this->service_message_push->send(GIVE_ONLOOKER, $v['uid'], '', array('#name#'=>$onlookers_conf['name']));
    			}
    		}
    		
    		$num = count($lists);
    		$data = array(
    					'offset'=>$offset + $num,
    					'end'=>($limit == $num ? 0 : 1),
    				);

            $this->json_output(array('code'=>0, 'data'=>$data));
    	}
    	
    	$this->load->view('admin/operate/onlookers/give', array('onlookers_conf'=>$onlookers_conf));
    }
}